<template>
	<view>
		<scroll-view scroll-y class="page">
			<image src="https://skv4.oss-cn-hangzhou.aliyuncs.com/cityCard/sapp/top-img.png"
			 mode="widthFix" class="response"></image>
			<view class="content">
				<view class="uni-form-item uni-column">
					<input type="number" class="uni-input" name="" v-model="mobile" placeholder="请输入手机号"/>
				</view>
				<view class="uni-form-item uni-column column-with-btn">
					<input type="text" class="uni-input" name="" placeholder="请输入图片验证码" v-model="captchaImg" />
					<image :src="verifyImg" mode="" class="img-captcha" @click="getVerify"></image>
				</view>
				<view class="uni-form-item uni-column column-with-btn">
					<input type="text" class="uni-input" name="" placeholder="请输入验证码" v-model="code"/>
					<button :class="{active : !disableCodeBtn}" :disabled="disableCodeBtn" @tap="sendCode">{{codeBtn.text}}</button>
				</view>
			
				<button class="bg-blue"  open-type='getUserInfo' @getuserinfo="changVerify">确定</button>
			</view>
			<view class="cu-tabbar-height"></view>
		</scroll-view>
	</view>
</template>

<script>
	import {verify,sendMsg,sappBingMobile} from '../../../../api/user.js'
	export default {
		data() {
			return {
				captchaImg: '',
				seconds: 10,
				mobile:'',
				key:'',
				verifyImg:'',
				code:'',
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				}
			}
		},
		onLoad(option) {
			let _this = this
			this.ticket = option.ticket
			setTimeout(function(){
				_this.getVerify()
			},1000)
			
		},
		methods: {
			sendCode: function () {
				if(this.key == '' || this.captchaImg == ''){
					this.$common.toast('请输入验证码')
					return false
				}
				this.codeBtn.waitingCode = true;
				this.codeBtn.count = this.seconds;
				this.codeBtn.text = this.codeBtn.count + 's';
				
				this.sendMobileMsg()
				
				let countdown = setInterval( () => {
					this.codeBtn.count--;
					this.codeBtn.text = this.codeBtn.count + 's';
					if( this.codeBtn.count < 0 ){
						clearInterval(countdown);
						this.codeBtn.text = '重新发送';
						this.codeBtn.waitingCode = false;
					}
				},1000);
			},
			gotoLogin: function () {
				uni.navigateTo({
					url: 'login'
				})
			},
			getVerify(){
				let _this = this
				verify().then(ret=>{
					if(ret.data.errcode == 0){
						_this.key = ret.data.data.key
						_this.verifyImg = ret.data.data.pic
					}
				})
			},
			sendMobileMsg(){
				let _this = this
				sendMsg(this.mobile,this.captchaImg,this.key).then(ret=>{
					if(ret.data.errcode == 0 && ret.data.data == 'chenggong'){
						_this.$common.toast('短信发送成功')
					}
				})
			},
			changVerify(info){
				console.log(info)
				let _this = this
				sappBingMobile(this.mobile,this.code,this.ticket,info.detail.encryptedData,info.detail.iv).then(ret=>{
					if(ret.data.status ==1 ){
						_this.$common.toast(ret.data.msg);
						uni.redirectTo({
							url: '/pages/index/menus'
						});
					}else{
						_this.$common.toast(ret.data.msg);
					}
				})
			}
		},
		computed: {
			disableCodeBtn: function (){
				return this.codeBtn.waitingCode || this.captchaImg.length < 4;
			} 
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #b49950;
	.content{
		padding: 60upx 100upx 100upx;
	}
	.logo{
	    text-align: center;
	}
	.uni-form-item{
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;
		.uni-input{
			font-size: 30upx;
			height: 1.8rem;
			padding: 7px 0;
		}
	}
	.column-with-btn{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		button{
			font-size: 24upx;
			margin: 0;
			width: 180upx;
			text-align: center;
			&:after{
				border: none
			}
			&.active{
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}
	.img-captcha{
		width: 150upx;
		height: 60upx;
	}
	// button[type="primary"]{
	// 	background-color: $color-primary;
	// 	border-radius: 0;
	// 	font-size: 34upx;
	// 	margin-top: 60upx;
	// }
	.links{
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;
		view{
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}
		.link-highlight{
			color: $color-primary
		}
	}
</style>
